package com.dongdongshop.controller;


import com.dongdongshop.data.Result;
import com.dongdongshop.service.CartService;
import com.dongdongshop.uitl.JwtUtil;
import com.dongdongshop.vo.CartVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("cart")
public class CartController {

    @Autowired
    private CartService cs;

    @Autowired
    private JwtUtil jwtUtil;


    //添加购物车
    @PostMapping("addCart")
    public Result addCart(@RequestHeader String AutharizeToken ,@RequestParam Long itemId, @RequestParam Integer num){
        Claims claims = jwtUtil.parseJwt(AutharizeToken);
        String userId = claims.getId();//登录的用户Id
        try {
            cs.addCart(userId,itemId,num);
        }catch (Exception e){
            return Result.ER();
        }

        return Result.OK();
    }

    //查询购物车
    @PostMapping("getCart")
    public Result getCart(@RequestHeader String AutharizeToken){
        Claims claims = jwtUtil.parseJwt(AutharizeToken);
        String userId = claims.getId();//登录的用户Id
        List<CartVo> list = cs.getCartListFromRedis(userId);
        return Result.OK().setData(list);
    }
}
